import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

typedef PAGCallback = void Function();

class PAGImageView extends StatefulWidget {
  final double? width;
  final double? height;

  /// 网络资源，动画链接
  final String? url;
  final String? assetName;

  /// 初始化后自动播放
  final bool autoPlay;

  /// 循环次数
  final int repeatCount;
  final double initProgress;

  /// 初始化完成
  final PAGCallback? onInit;

  /// Notifies the start of the animation.
  final PAGCallback? onAnimationStart;

  /// Notifies the end of the animation.
  final PAGCallback? onAnimationEnd;

  /// Notifies the cancellation of the animation.
  final PAGCallback? onAnimationCancel;

  /// Notifies the repetition of the animation.
  final PAGCallback? onAnimationRepeat;

  /// 加载失败时的默认控件构造器
  final Widget Function(BuildContext context)? defaultBuilder;

  static const int REPEAT_COUNT_LOOP = -1; //无限循环
  static const int REPEAT_COUNT_DEFAULT = 1; //默认仅播放一次

  const PAGImageView.network(
    this.url, {
    this.width,
    this.height,
    this.repeatCount = REPEAT_COUNT_LOOP,
    this.autoPlay = true,
    this.onInit,
    this.onAnimationStart,
    this.initProgress = 0.0,
    this.onAnimationEnd,
    this.onAnimationCancel,
    this.onAnimationRepeat,
    this.defaultBuilder,
    super.key,
    this.assetName,
  });

  const PAGImageView.asset(
    this.assetName, {
    this.width,
    this.height,
    this.repeatCount = REPEAT_COUNT_DEFAULT,
    this.autoPlay = true,
    this.onInit,
    this.initProgress = 0.0,
    this.onAnimationStart,
    this.onAnimationEnd,
    this.onAnimationCancel,
    this.onAnimationRepeat,
    this.defaultBuilder,
    super.key,
    this.url,
  });

  @override
  PAGImageViewState createState() => PAGImageViewState();
}

class PAGImageViewState extends State<PAGImageView> {
  MethodChannel? _channel;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: widget.width,
      height: widget.height,
      child: AndroidView(
          viewType: 'plugins/pag',
          hitTestBehavior: PlatformViewHitTestBehavior.transparent,
          creationParams: {
            'viewType': 1,
            'url': widget.url,
            'assetName': widget.assetName,
            'repeatCount': widget.repeatCount,
            'autoPlay': widget.autoPlay,
          },
          onPlatformViewCreated: (id) {
            _channel = MethodChannel('plugins/pag_$id');
            _channel?.setMethodCallHandler(callHandler);
          },
          creationParamsCodec: const StandardMessageCodec()),
    );
  }

  start() {
    _channel?.invokeMethod("start");
  }

  stop() {
    _channel?.invokeMethod("stop");
  }

  Future callHandler(MethodCall call) async {
    switch (call.method) {
      case "onInit":
        widget.onInit?.call();
      case "onAnimationStart":
        widget.onAnimationCancel?.call();
        break;
      case "onAnimationEnd":
        widget.onAnimationEnd?.call();

        break;
      case "onAnimationCancel":
        widget.onAnimationCancel?.call();
        break;
      case "onAnimationRepeat":
        widget.onAnimationRepeat?.call();

        break;
    }
  }

  @override
  void dispose() {
    super.dispose();
    _channel?.setMethodCallHandler(null);
  }
}
